<?php

namespace app\index\controller\sunfc;

use app\index\controller\Base;
use app\index\model\guo\DdUsersBalanceLog;
use think\Db;

class Index extends Base
{
    //积分日志插入演示使用
    public function text_in_log()
    {
        $log_model = new DdUsersBalanceLog();

        $data = [
            'uid'  =>  1,
            'sign' =>  1,
            'number'  =>  50,
            'content'  =>  "签到",
            'type'  =>  1,
            'create_time'  => time(),
        ];
        if($log_model->insert_log($data)){
            dump(1).die;
        }else{
            dump(0).die;
        }
    }
    //展示会员积分信息
    public function show()
    {
        // 查询用户信息
        $result = Db::name('dd_users')->where('uid',$this->uid)->find();
        $qiniuyun = "?imageMogr2/auto-orient/thumbnail/!320x240r/gravity/Center/crop/320x240/quality/100/interlace/0";
        $result['headimgurl'] = $result['headimgurl'].$qiniuyun;
        $this->assign('result',$result);
        // 查询不同的积分数目
        $sign = Db::name('dd_users_balance_log')
            ->where('uid',$this->uid)
            ->where('sign',1)
            ->where('type',1)
            ->field('number')
            ->select();

        $sign_sum = 0;//定义一个求和后的变量
        foreach($sign as $key => $item)
        { //foreach 遍历数组$key 是键值,$item 是元素值
            $sign_sum += $sign[$key]['number']; //这句和 $sum = $sum + $item;是一个意思
        }
       // dump($sign_sum);die();
        $this->assign('sign_sum',$sign_sum);
        // 展示不同分类对应的积分数目
        $dat = Db::name('option')->where('option_name','point_set')->value('option_value');
        $data=json_decode($dat, true);
        $this->assign('data',$data);
        return $this->fetch();

    }

    //展示积分日志信息
    public function show_log(){
        if(request()->isAjax()){
             //暂时使得UID为1
            $uid = $this->uid;

            $page = input('page') ? input('page') : 1;
            $map["uid"] = $uid;
            $map["type"] = 1;
            //拿到uid为1的用户的积分，分页为10条数据为一页
            $res = Db::name("dd_users_balance_log")
                ->where($map)->page($page,10)
                ->order("create_time","desc")
                ->select();
            //对不符合输出规范的数据进行处理
            foreach ($res as $index => $re) {
                if(strtotime(date('Y-m-d',time()))<$res[$index]['create_time']){
                    $res[$index]['create_time'] = date('H:i', $re['create_time']);
                }elseif (strtotime(date("Y-m-d",strtotime("-1 day")))<$res[$index]['create_time']){
                    $res[$index]['create_time'] = "昨天";
                }else{
                    $res[$index]['create_time'] = "以前";
                }

                if($res[$index]['sign']==1){
                    $res[$index]['sign'] = "+";
                }else{
                    $res[$index]['sign'] = "-";
                }
            }
            return json(['code' => 1, 'msg' => 'ok','data' => $res]);

        }else{
            $this->assign('back_url',url('index/sunfc.users/index'));
            return $this->fetch();

        }


    }

    //展示文章信息
    public function show_article(){
        $res = Db::name("article")->where("is_del",0)->where("is_show",1)
            ->order("create_time","desc")->select();
        dump($res).die;
    }

    //根据分类展示文章信息
    public function show_class_article(){
        //暂时假定分类id为1
        $class_id = 1;
        $res = Db::name("article")->where("is_del",0)->where("is_show",1)
            ->where("class_id",$class_id)->order("create_time","desc")->select();
        dump($res).die;
    }
}
